<template>
    <div class="home_Follow">
        <div class="follow" v-for="(value,index) in followList" :key="index">
            <img class="follow_avater" :src="value.follow.avatar" @click="toAuthor(value.follow['_id'])"/>
            <p class="follow_name" @click="toAuthor(value.follow['_id'])">{{value.follow.name}}</p>
            <p class="follow_date">关注于：{{value.date | formateDate}}</p>
            
            <div class="follow_button">
                <el-button size="mini">私信</el-button>
                <el-button size="mini" v-cloak @click="canserFollow(value.follow['_id'])">取消关注</el-button>
            </div>
        </div>
    </div>
</template>

<script>
import { Message } from 'element-ui';
export default {
    name:'HomeMain',
    props:['userLogin','userChoosed'],
    data() {
      return {
        followList:[],
      };
    },
    methods: {
        getFollows(userId){
            this.$axios.post('/api/follows/getUserFollows',{
                user:userId,
            })
            .then(res=>{
                this.followList=res.data.data; 
            })
        },
        //跳转到作者页面
        toAuthor(authorId){
            this.$router.push(`/home/${authorId}`);
        },
        canserFollow(id){
            this.$axios.post('/api/follows/canserFollow',{
                follow:id,
            })
            .then(res=>{
                if(res.data.ok==1){
                    Message.error("出错了！");
                }
                else{
                    let index=0;
                    for(let i=0;i<this.followList.length;i++){
                        if(this.followList[i].follow['_id']){
                            index=i;
                            break;
                        }
                    }
                    this.followList.splice(index,1);
                    Message.success({message:"已取消关注~~~",showClose:true});
                }
            })
        }
    },
    created(){
        if(this.userChoosed.id){
            this.getFollows(this.userChoosed.id);
        }
    },
    watch:{
        userChoosed:function(){
            this.getFollows(this.userChoosed.id);
        },
    }
}
</script>

<style scoped>
.home_Follow{
    width:100%;
    /* height:100px; */
    box-shadow: 0 1px 2px 0 rgba(0,0,0,.04);
    border:1px solid #ccc;
    padding:5px 5px 10px 5px;
    box-sizing: border-box;
    position: relative;
}
.follow{
    padding:15px;
    box-sizing: border-box;
}

.follow_avater{
    width:30px;
    height:30px;
    border-radius: 50%;
    float:left;
    cursor:pointer;
    background-size: cover;
}
.follow_name{
    font-size: 12px;
    color:#666;
    margin:5px 45px;
    cursor: pointer;
}
.follow_date{
    font-size:10px;
    color:#858585;
    margin-right:20px;
    margin:5px 45px;
}
.follow_button{
    float:right;
    position: relative;
    top:-25px;
    right:0
}
.el-button{
    float:right;
    margin-right:20px;
}
.el-button:hover{
    background:#f21f2b;
    color:#fff;
}
a{
    text-decoration: none;
    color:#000;
}
</style>

